<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="userprofile.photo" @click="fileshow"/>
          <input type="file" name="" id="" ref="iptfile" accept="image/*" @change="updatephoto">
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="userprofile.name" @click="updatename"/>
      <van-cell title="生日" is-link :value="userprofile.birthday" @click="birthdayfn" />
    </van-cell-group>
        <!-- 更改用户名 -->

          <van-dialog v-model="diashow" title="请输入新昵称" show-cancel-button :before-close="confirmclose">
          <input type="text" v-model="iptvalue" placeholder="请输入昵称" v-iptfocus>
          </van-dialog>
     <!-- 更改生日 -->
     <van-popup v-model="popshow" position="bottom" round :style="{ height: '50%' }">
         <van-datetime-picker
         v-model="currentDate"
         type="date"
         title="选择生日"
         :min-date="minDate"
         :max-date="maxDate"
         @confirm="updatebirfn"
         @cancel="cancelfn"
      />
     </van-popup>

  </div>
</template>

<script>
import { userprofileAPI, updataphotoAPI, updatenameAPI } from '@/api'

import { Mytitle } from '@/utils/Notify.js'
// import moment from 'moment'
import { mapMutations } from 'vuex'

export default {
  name: 'UserEdit',
  async created () {
    const res = await userprofileAPI()
    this.userprofile = res.data.data
    // this.setuseObj(res.data.data)
  },
  data () {
    return {
      userprofile: {}, // 个人信息 生日 头像 用户名
      popshow: false,
      diashow: false, // 是否显示弹窗
      iptvalue: '',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  methods: {
    ...mapMutations(['setUseimg', 'setUsename']),
    //   点击头像 上传图片
    fileshow () {
      // 点击图片,触发file上传
    //   document.querySelector('input[type=file]').click()===如果页面有多个上传文件的ipt就不行了
      this.$refs.iptfile.click()
    },
    // 文件选择后的change事件
    async updatephoto (e) {
      const fromobj = new FormData()
      fromobj.append('photo', e.target.files[0])
      const res = await updataphotoAPI(fromobj)// 数据回来之后,需要让页面的头像同步更新
      this.userprofile.photo = res.data.data.photo
      this.setUseimg(res.data.data.photo)
    },
    // 更新昵称
    updatename () {
      this.diashow = true// 点击用户,弹出框进行修改
      this.iptvalue = this.userprofile.name
    },
    async confirmclose (action, done) {
      // 修改后的值,要进行正则
      //   /^[A-Za-z0-9-\u4e00-\u9fa5]{6,9}$/
      if (action === 'confirm') {
        // 校验,校验通过 done() 校验不通过 done(false) 提示用户昵称不合格
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.iptvalue)) {
          done()
          // 校验成功之后,掉接口
          await updatenameAPI({ usename: this.iptvalue })
          // 将页面数据更新
          this.userprofile.name = this.iptvalue
          this.setUsename(this.iptvalue)
        } else {
          done(false)
          Mytitle({ type: 'warning', message: '昵称不合格' })
        }
      } else if (action === 'cancel') {
        done()
      }
    },
    // 更新生日
    birthdayfn () {
      this.popshow = true
      //  this.currentDate  后台要"年-月-日"格式字符串
      // await updatenameAPI({ birthday: })
      //   组件要传入的是日期对象
      this.currentDate = new Date(this.userprofile.birthday)
    },
    async updatebirfn () {
      // console.log(value)// value 是当前选择的事件对象
    //   const res = moment(this.currentDate).format('YYYY-MM-DD')
      const Y = this.currentDate.getFullYear()
      const M = this.currentDate.getMonth() + 1
      const D = this.currentDate.getDate()
      const res = `${Y}-${M}-${D}`
      console.log(Y, M, D)
      await updatenameAPI({ birthday: res })
      this.userprofile.birthday = res
      this.popshow = false
    },
    cancelfn () {
      this.popshow = false
    }

  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .action-card input{
    display: none;
//将上传文件按钮隐藏
}
/deep/ .van-dialog__content{
    text-align: center;
    input{
       text-align: center;
       border:none;
       margin: 15px 0 10px;
    }
}
</style>
